package com.example.arrange;

import com.example.util.CommonUtils;

import java.util.concurrent.CompletableFuture;

public class AnyOfDemo {
    public static void main(String[] args) {
        CommonUtils.printThreadLog("main start");

        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            CommonUtils.sleepSecond(3);
            return "Future1 的结果";
        });
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            CommonUtils.sleepSecond(4);
            return "Future2 的结果";
        });
        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
            CommonUtils.sleepSecond(2);
            return "Future3 的结果";
        });
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future1, future2, future3);

        CommonUtils.printThreadLog("main continue");

        Object result = anyOf.join();
        System.out.println(result);

        CommonUtils.printThreadLog("main end");
    }
}
